/**
 *
 */
package vn.edu.hungvuongaptech.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;

/**
 * @author ThanhHQ
 *
 */
public class ApproveTimeSheetController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String ids[] = req.getParameterValues("chkTimeSheetID");
		String btnApprove = req.getParameter("btnApprove");
		String btnUnapprove = req.getParameter("btnUnapprove");
		int statusId = 1;
		if (btnApprove != null)
			statusId = 2;
		else if (btnUnapprove != null)
			statusId = 3;
		String nextPath = "";
		if (ids != null) {
			int count = 0;
			for (int i = 0; i < ids.length; ++i) {
				TimeSheetDAO.updateTimeSheetStatusById(Long.parseLong(ids[i]), statusId);
				++count;
			}
			if (count > 0) {
				if (statusId == 2) // Approve
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Selected Timesheet's records are approved successfully!";
				else // Reject
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Selected Timesheet's records are rejected successfully!";
			} else {
				if (statusId == 2) // Unable to approve.
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Unable to approve selected Timesheet's records!";
				else // Unable to reject.
					nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Unable to reject selected Timesheet's records!";
			}
		} else {
			if (statusId == 2) // approve.
				nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Please select Timesheet's records to approve!";
			else // reject.
				nextPath = Constant.PATH_RES.getString("timesheet.admin.messagePath") + "?msg=Please select Timesheet's records to reject!";
		}
		resp.sendRedirect(nextPath);
	}
}
